package ihm;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import data.Chrono;
import data.Temps;

public class FenetreRegle extends JFrame {
	Chrono c;
	
	public FenetreRegle(Chrono chronopartie){
		super("Reglage");
		setTitle("Murder Party - Modifier le temps de jeu de la partie");
		this.c = chronopartie;
		build();
	}
	
	private void build(){
		/** declaration */
		JPanel pnlmain = new JPanel();
		JLabel lblchrono = new JLabel("Nouveau temps de jeu de la partie restant (en s)");
		lblchrono.setEnabled(false);
		JTextField txtchrono = new JTextField();
		JButton btnchrono = new JButton("Valider");
		
		/** config */
		txtchrono.setPreferredSize(new Dimension(120, 30));
		btnchrono.addActionListener(new ActionListenerChrono(txtchrono, c));
		
		/** assemblage */
		pnlmain.add(lblchrono);
		pnlmain.add(txtchrono);
		pnlmain.add(btnchrono);
		getContentPane().add(pnlmain);
		pack();
		
	}
	
	class ActionListenerChrono implements ActionListener {

		JTextField txtchrono;
		Chrono c;
		public ActionListenerChrono(JTextField txtchrono, Chrono c){
			this.txtchrono = txtchrono;
			this.c = c;
		}
		
		public void actionPerformed(ActionEvent e) {
			String s = txtchrono.getText();
			long l = Long.decode(s);
			if(l > 0){
				System.out.println("[Admin][FntreReglage] set chrono to "+l+" secondes");
				c.setChrono(new Temps(l));
			}
			dispose();
		}
		
	}
}
